﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessTier;
using System.Threading;
using DataTier;
using System.Threading.Tasks;

namespace AdminClient
{
    public partial class MainForm : Form
    {

        private PersonCtr _personCtr = PersonCtr.Instance;
        private StationCtr _stationCtr = StationCtr.Instance;
        private ReservationCtr _reservationCtr = ReservationCtr.Instance;
        private LocationCtr _locationCtr = LocationCtr.Instance;

        private Task _updateStationsTask = null;
        private Task _updateCustomersTask = null;
        private Task _updateReservationsTask = null;
        private Task _updateEmployeesTask = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void UpdateCustomers()
        {

            btnRefreshCustomers.Enabled = false;

            if (_updateCustomersTask == null)
            {

                _updateCustomersTask = new Task(() =>
                {

                    IList<Customer> customers = _personCtr.GetAllCustomers();
                    Thread.Sleep(2000);

                    _updateCustomersTask = null;

                    this.Invoke(new Action(() =>
                    {
                        lstCustomers.DataSource = customers;
                        btnRefreshCustomers.Enabled = true;
                    }));

                });

                _updateCustomersTask.Start();

            }

        }

        private void UpdateReservations()
        {

            btnRefreshReservations.Enabled = false;

            if (_updateReservationsTask == null)
            {

                _updateReservationsTask = new Task(() =>
                {

                    IList<Reservation> reservations = _reservationCtr.GetAllReservations();
                    Thread.Sleep(2000);

                    _updateReservationsTask = null;

                    this.Invoke(new Action(() =>
                    {
                        lstReservations.DataSource = reservations;
                        btnRefreshReservations.Enabled = true;
                    }));

                });

                _updateReservationsTask.Start();

            }

        }

        private void UpdateStations()
        {

            btnRefreshStations.Enabled = false;

            if (_updateStationsTask == null)
            {

                _updateStationsTask = new Task(() =>
                {

                    IList<Station> stations = _stationCtr.GetAllStations();
                    IList<Location> locations = _locationCtr.GetAllLocations();
                    Thread.Sleep(2000);

                    _updateStationsTask = null;

                    this.Invoke(new Action(() =>
                    {
                        lstStations.DataSource = stations;
                        cmbStationLocation.DataSource = locations;
                        btnRefreshStations.Enabled = true;
                    }));

                });

                _updateStationsTask.Start();

            }

        }

        private void UpdateEmployees()
        {

            btnRefreshEmployees.Enabled = false;

            if (_updateEmployeesTask == null)
            {

                _updateEmployeesTask = new Task(() =>
                {

                    IList<Customer> customers = _personCtr.GetAllCustomers();
                    Thread.Sleep(2000);

                    _updateEmployeesTask = null;

                    this.Invoke(new Action(() =>
                    {
                        lstCustomers.DataSource = customers;
                        btnRefreshEmployees.Enabled = true;
                    }));

                });

                _updateEmployeesTask.Start();

            }

        }

        private void btnRefreshCustomers_Click(object sender, EventArgs e)
        {
            UpdateCustomers();
        }

        private void btnAddCustomer_Click(object sender, EventArgs e)
        {

            try
            {
                _personCtr.AddCustomer(txtFirstName.Text, txtLastName.Text, txtAddress.Text, Convert.ToInt32(txtZip.Text), txtEmail.Text, txtPhone.Text, txtMembership.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not add customer. Error: " + ex.ToString());
            }

        }

        private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
        {

            Customer selectedCustomer = (Customer)lstCustomers.SelectedItem;

            txtAddress.Text = selectedCustomer.Address;
            txtEmail.Text = selectedCustomer.Email;
            txtFirstName.Text = selectedCustomer.FirstName;
            txtLastName.Text = selectedCustomer.LastName;
            txtMembership.Text = selectedCustomer.Membership;
            txtPhone.Text = selectedCustomer.Phone;
            txtZip.Text = selectedCustomer.ZipCode.ToString();
            txtCity.Text = selectedCustomer.City;

        }

        private void btnDeleteCustomer_Click(object sender, EventArgs e)
        {

            if (lstCustomers.SelectedItem != null)
            {

                try
                {
                    Customer customerToDelete = (Customer)lstCustomers.SelectedItem;
                    _personCtr.RemoveCustomer(customerToDelete.CustomerId);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to delete customer. Error: " + ex.ToString());
                }

            }

        }

        private void btnRefreshStations_Click(object sender, EventArgs e)
        {
            UpdateStations();
        }

        private void lstStations_SelectedIndexChanged(object sender, EventArgs e)
        {

            Station selectedStation = (Station)lstStations.SelectedItem;

            txtStationAddress.Text = selectedStation.Address;
            txtStationBatteries.Text = selectedStation.AvailableBatteries.ToString();
            txtStationCity.Text = selectedStation.City;
            txtStationZip.Text = selectedStation.ZipCode.ToString();

            cmbStationLocation.SelectedItem = selectedStation.Location;
            cmbStationLocation.SelectedIndex = cmbStationLocation.Items.IndexOf(selectedStation.Location);

        }

        private void btnAddStation_Click(object sender, EventArgs e)
        {

            try
            {

                _stationCtr.AddStation(txtStationAddress.Text, Convert.ToInt32(txtStationZip.Text), Convert.ToInt32(txtStationBatteries.Text), (Location)cmbStationLocation.SelectedItem);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not add station. Error: " + ex.ToString());
            }

        }

        private void btnRefreshEmployees_Click(object sender, EventArgs e)
        {
            UpdateEmployees();
        }

        private void btnAddEmployee_Click(object sender, EventArgs e)
        {

            try
            {

                _personCtr.AddEmployee(txtEmpFirstName.Text, txtEmpLastName.Text, txtEmpAddress.Text, Convert.ToInt32(txtEmpZip.Text), txtEmpEmail.Text, txtEmpPhone.Text);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not add employee. Error: " + ex.ToString());
            }

        }

        private void lstEmployees_SelectedIndexChanged(object sender, EventArgs e)
        {

            Employee selectedEmployee = (Employee)lstEmployees.SelectedItem;

            txtEmpAddress.Text = selectedEmployee.Address;
            txtEmpCity.Text = selectedEmployee.City;
            txtEmpEmail.Text = selectedEmployee.Email;
            txtEmpFirstName.Text = selectedEmployee.FirstName;
            txtEmpLastName.Text = selectedEmployee.LastName;
            txtEmpPhone.Text = selectedEmployee.Phone;
            txtEmpZip.Text = selectedEmployee.ZipCode.ToString();

        }

        private void btnRefreshReservations_Click(object sender, EventArgs e)
        {
            UpdateReservations();
        }

        private void lstReservations_SelectedIndexChanged(object sender, EventArgs e)
        {

            Reservation selectedReservation = (Reservation)lstReservations.SelectedItem;
            Customer customer = selectedReservation.Customer;

            txtResId.Text = selectedReservation.ReservationNr.ToString();
            txtResCustomerAddress.Text = customer.Address;
            txtResCustomerId.Text = customer.CustomerId.ToString();
            txtResCustomerLastName.Text = customer.LastName;
            txtResCustomerName.Text = customer.FirstName;
            txtResCustomerPhone.Text = customer.Phone;
            txtResCustomerZip.Text = customer.ZipCode.ToString();
            txtResCustomerEmail.Text = customer.Email;
            txtResCustomerCity.Text = customer.City;


        }

    }
}
